﻿Public Class PrintForm
    Dim pageset As Printing.PageSettings = New Printing.PageSettings()
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        PageSetupDialog1.Document = PrintDocument1 ''设置Document属性=文档对象或者与PageSettings类关联就可以使用该控件
        PageSetupDialog1.AllowMargins = True
        PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings
        Dim re As DialogResult = PageSetupDialog1.ShowDialog()
        If re = Windows.Forms.DialogResult.OK Then
            pageset = PageSetupDialog1.PageSettings
        End If
    End Sub
    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        Dim mypen As Pen = New Pen(Color.Blue, 2)
        PrintDocument1.DefaultPageSettings = pageset
        e.Graphics.DrawString(RichTextBox1.Text, New Font("黑体", 8), mypen.Brush, 10, 10)
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        PrintDialog1.Document = PrintDocument1
        PrintDialog1.PrinterSettings.Copies = 5 ''打印的份数
        PrintDialog1.PrinterSettings.FromPage = 1 ''第一页的页码
        PrintDialog1.PrinterSettings.ToPage = 5 ''最后一页页码
        Dim re As DialogResult = PrintDialog1.ShowDialog()
        If re = Windows.Forms.DialogResult.OK Then
            AddHandler PrintDocument1.PrintPage, AddressOf Me.PrintDocument1_PrintPage
            PrintDocument1.Print()
        End If
    End Sub
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        PrintPreviewDialog1.Document = PrintDocument1
        AddHandler PrintDocument1.PrintPage, AddressOf Me.PrintDocument1_PrintPage
        PrintPreviewDialog1.PrintPreviewControl.Zoom = 1.0
        PrintPreviewDialog1.WindowState = FormWindowState.Normal
        PrintPreviewDialog1.ShowDialog()
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim PrintPreviewControl1 As PrintPreviewControl = New PrintPreviewControl
        PrintPreviewControl1.Name = "PrintPreviewControl1"
        PrintPreviewControl1.Dock = DockStyle.Fill
        PrintPreviewControl1.Document = PrintDocument1
        PrintPreviewControl1.UseAntiAlias = True
        AddHandler PrintDocument1.PrintPage, AddressOf Me.PrintDocument1_PrintPage
        Form2.Controls.Add(PrintPreviewControl1)
        PrintPreviewControl1.Show()
        Form2.Show()
    End Sub
End Class